<template>
  <el-popover v-model:visible="showPopover"
              placement="bottom"
              width="320"
              trigger="click"
              popper-class="no-padding-popover">
    <div v-loading="loading" class="common-words">
      <div v-empty="list && list.length == 0" xs-empty-icon="none" class="word">
        <div v-for="(item, index) in list" :key="index" class="word-item" v-on:click="selectScene(item, index)">
          {{ item }}
        </div>
      </div>
      <div class="handle-interval">
        <flexbox class="handle-button" @click="setClick">
          <i class="wk wk-manage handle-button-icon"/>
          <div class="handle-button-name" >管理</div>
        </flexbox>
      </div>
    </div>
    <template v-slot:reference>
      <el-button class="word-button">常用语</el-button>
    </template>
    <Set v-model:visible="setShow" :list="list" @update="udpateCommonWords"/>
  </el-popover>
</template>

<script setup>

import Set from './Set.vue'
import {ref, watch, onMounted} from "vue"
import {sysConfigQueryPhraseAPI} from "@/api/crm/common.js";

const loading = ref(false)
const showPopover = ref(false)
const setShow = ref(false)
const list = ref([])

defineOptions({
  // 常用语
  name: 'CommonWords',
})
const emits = defineEmits(['select'])

watch(showPopover, (val) => {
  if (val && list.value.length == 0) {
    getCommonWords()
  }
})

onMounted(() => {
  getCommonWords()
})

function selectScene(item, index) {
  emits('select', item)
  showPopover.value = false
}

function setClick() {
  console.log("setClick")
  nextTick(() => {
    setShow.value = true
    console.log("setShow",setShow)
  })


}

/**
 * 获取常用语
 */
function getCommonWords() {
  loading.value = true
  sysConfigQueryPhraseAPI().then(res => {
    loading.value = false
    list.value = res.data.data || []
  }).catch(() => {
    loading.value = false
  })
}

/**
 * 更新欢迎语
 */
function udpateCommonWords(data) {
  list.value = data
}
</script>


<style lang="scss" scoped>
.common-words {
  position: relative;
}

.word {
  height: 144px;
  border-radius: 4px;
  position: relative;
  overflow-y: auto;
  font-size: 12px;
  margin-bottom: 10px;

  .word-item {
    color: #333;
    padding: 10px 15px;
    cursor: pointer;
    background-color: white;
  }

  .word-item:hover {
    background-color: #f7f8fa;
    color: $xr-color-primary;
  }
}


.handle-button {
  padding: 6px 20px;
  font-size: 12px;
  cursor: pointer;
  color: $xr-color-primary;

  .handle-button-icon {
    margin-right: 8px;
    margin-top: 3px;
  }

  .handle-button-name {
    font-size: 12px;
  }
}

.handle-button:hover {
  .handle-button-name {
    text-decoration: underline;
  }
}

.handle-interval {
  border-top: 1px solid #EFEFEF;
}

.word-button {
  padding: 10px 12px;
}
</style>
